package com.wisdytech.linkdcs.configuration.action;


import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.bean.StandardResult;
import com.wisdytech.common.bean.StandardTableResult;
import com.wisdytech.common.handler.ResourceMatchingService;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.linkdcs.configuration.model.TtEqConfiguration;
import com.wisdytech.linkdcs.configuration.service.ITtEqConfigurationService;
import com.wisdytech.linkdcs.system.model.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chenbin
 * @since 2018-08-27
 */
@Controller
@RequestMapping("/equipment/configuration/threed")
public class ConfigurationThreedAction {


    @Resource
    private ITtEqConfigurationService ttEqConfigurationService;

    @Value("${scada-3D.design-url}")
    private String scada3DDesignUrl;
    @Value("${scada-3D.show-url}")
    private String scada3DShowUrl;
    @Autowired
    @Qualifier("resourceMatchingService")
    private ResourceMatchingService resourceMatchingService;


    /**
     *
     * <p>Description: 车间组态</p>
     * @param model model
     * @return String
     */
    @RequestMapping("/shop/index")
    public String shopIndex(Model model, HttpServletRequest httpRequest){
        this.resourceMatchingService.getResourceName(httpRequest,model);
        return "monitorManage/workshopConfiguration/indexThreeD";
    }

    /**
     *
     * <p>Description: 产线组态</p>
     * @param model model
     * @return String
     */
    @RequestMapping("/line/index")
    public String lineIndex(Model model, HttpServletRequest httpRequest){
        this.resourceMatchingService.getResourceName(httpRequest,model);
        return "monitorManage/productionLineConfiguration/indexThreeD";
    }

    /**
     *
     * <p>Description: 工厂组态</p>
     * @param model model
     * @return String
     */
    @RequestMapping("/factory/index")
    public String factoryIndex(Model model, HttpServletRequest httpRequest){
        this.resourceMatchingService.getResourceName(httpRequest,model);
        return "monitorManage/factoryAreaConfiguration/indexThreeD";
    }

    /**
     * 组态设计页面
     * @param model
     * @param type
     * @return
     */
    @RequestMapping("/design/index")
    public String configurationDesignIndex(Model model,
                                           @RequestParam(value="type",required=false, defaultValue = "0") String type,
                                           String fileKey) {


        //添加展开的等级
        model.addAttribute("level1", "设备管理");
        model.addAttribute("level2", "监控管理");
        switch(type) {
            case "3":
                model.addAttribute("level3", "3D车间组态管理");
                model.addAttribute("title1", "3D车间组态管理");
                model.addAttribute("title2", "3D车间组态设计");

                break;
            case "2":
                model.addAttribute("level3", "3D产线组态管理");
                model.addAttribute("title1", "3D产线组态管理");
                model.addAttribute("title2", "3D产线组态设计");
                break;
            case "1":
                model.addAttribute("level3", "3D厂区组态管理");
                model.addAttribute("title1", "3D厂区组态管理");
                model.addAttribute("title2", "3D厂区组态设计");
                break;
        }
        model.addAttribute("level4", "");

        //组态记录id
        model.addAttribute("fileKey", fileKey);

        //组态记录
        Map<String, Object>  configuration =
                ttEqConfigurationService.getConfigurationById(fileKey);

        if(null == configuration) {
            //错误页面
            return "login";
        }
        model.addAttribute("configuration", configuration);

        //组态设计地址
        model.addAttribute("url", scada3DDesignUrl);

        return "monitorManage/configurationDesign/index";
    }

    @RequestMapping("/monitor/index")
    public String configurationMonitorIndex(Model model,
                                            @RequestParam(value="type",required=false, defaultValue = "0") String type) {
        //添加展开的等级
        model.addAttribute("level1", "设备管理");
        model.addAttribute("level2", "监控管理");
        switch(type) {
            case "3":
                model.addAttribute("level3", "3D车间组态管理");
                model.addAttribute("title1", "3D车间组态管理");
                model.addAttribute("title2", "3D车间组态设计");

                break;
            case "2":
                model.addAttribute("level3", "3D产线组态管理");
                model.addAttribute("title1", "3D产线组态管理");
                model.addAttribute("title2", "3D产线组态设计");
                break;
            case "1":
                model.addAttribute("level3", "3D厂区组态管理");
                model.addAttribute("title1", "3D厂区组态管理");
                model.addAttribute("title2", "3D厂区组态设计");
                break;
        }


        model.addAttribute("level3", "3D实时监控");
        model.addAttribute("level4", "");

        //组态设计地址
        model.addAttribute("url", scada3DShowUrl);

        return "monitorManage/realTimeMonitoring/indexThreeD";
    }


    /**
     *
     * @param type 类型
     * @param code 编码
     * @param locationId 关联id
     * @param filter 分页参数
     * @return StandardTableResult
     */
    @RequestMapping(value = "/listConfigurationByFilter/get.mvc")
    @ResponseBody
    public StandardTableResult<Map<String, Object>> listConfigurationByFilter(String type, String code, String factoryId, String locationId, Filter filter) {
        String configurationType = "2";
        PageInfo<Map<String, Object>> page = this.ttEqConfigurationService.listConfigurationByFilter(type,code,factoryId,locationId,filter,configurationType);
        return new StandardTableResult<>(page.getTotal(),page.getList());
    }


    /**
     * 根据localtionId获取全部的组态记录
     * @param pkId
     * @return
     */
    @RequestMapping(value = "/listAllConfiguration/get.mvc")
    @ResponseBody
    public StandardResult listConfiguration(String pkId){
        String configurationType = "2";
        List<TtEqConfiguration>  data =ttEqConfigurationService.listConfiguration(pkId,configurationType);
        if(null == data || data.size()==0) {
            return StandardResult.getFailResult();
        }
        return StandardResult.getSuccessResult(data);
    }


    /**
     *
     * @param ttEqConfiguration 实体类
     * @return StandardResult
     */
    @RequestMapping(value = "/saveOrUpdateConfiguration/save.mvc")
    @ResponseBody
    public StandardResult saveOrUpdateConfiguration(TtEqConfiguration ttEqConfiguration) {
        SysUser user = BaseUtil.getCurrentUser();
        ttEqConfiguration.setConfigurationType("2");//3D组态
        this.ttEqConfigurationService.saveOrUpdateConfiguration(ttEqConfiguration,user);
        return StandardResult.getSuccessResult();
    }

    /**
     * 根据pkId查询组态信息
     * @param pkId 设备code
     * @return StandardResult
     */
    @RequestMapping(value = "/getConfigurationById/get.mvc")
    @ResponseBody
    public StandardResult getConfigurationById(String pkId) {
        Map<String, Object> configuration = ttEqConfigurationService.getConfigurationById(pkId);
        return StandardResult.getSuccessResult(configuration);
    }

    /**
     *
     * @param pkIds 主键
     * @return StandardResult
     */
    @RequestMapping(value = "/deleteConfiguration/del.mvc")
    @ResponseBody
    public StandardResult deleteConfiguration(String pkIds) {
        this.ttEqConfigurationService.deleteConfiguration(pkIds);
        return StandardResult.getSuccessResult();
    }



}

